<template>
    <list-layout>
        <div slot="search">
            <el-form :model="queryParams" :inline="true" ref="queryForm" v-show="showSearch" label-width="88px">
                <el-row>
                    <el-col :span="6">

                        <el-form-item :label="$t('formValidation.samplingPlanName')" prop="processName">
                            <el-input v-model="queryParams.planName" :placeholder="$t('public.placeholder',{placeholder: $t('formValidation.samplingPlanName')})" clearable size="small" @keyup.enter.native="handleQuery" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="6" align="left">
                        <el-form-item class="right-btns">
                            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('public.query')}}</el-button>
                            <el-button icon="el-icon-refresh" class="resectBtn" size="mini" @click="resetQuery">{{$t('public.reset')}}</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>

        </div>
        <div class="app-container">
            <div class="container-header">
                <div class="header-left">
                    {{ $t('inventoryManagement.samplingScheme') }}
                </div>
            </div>
            <el-row :gutter="10" class="table-action-wrap btnGroup">
                <el-col :span="1.5">
                    <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">{{ $t('btn.add') }}
                    </el-button>
                </el-col>

            </el-row>

            <el-table border stripe v-loading="loading" :data="processList" >
                <!-- <el-table-column type="selection" width="55" align="center" /> -->
                <el-table-column :label="$t('public.serialNum')" type="index" align="center">
                    <template slot-scope="scope">
                        <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
                    </template>
                </el-table-column>

                <el-table-column :label="$t('formValidation.samplingPlanName')" align="center" min-width="180" prop="planName" show-overflow-tooltip />
                <el-table-column :label="$t('formValidation.affiliatedBusinessDepartment')" align="center" prop="divisionName" show-overflow-tooltip />

                <el-table-column :label="$t('public.opera')" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <div class="action-wrap">
                            <table-body-btn type="gwedit" @click.native="handleUpdate(scope.row)"   />
                            <table-body-btn type="gwdelete" @click.native="handleDelete(scope.row)"   />
                        </div>
                    </template>
                </el-table-column>

            </el-table>

            <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" layout="total, sizes, prev, pager, next, jumper" />

            <!-- 添加或修改产品线对话框 -->
            <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :fullscreen="fullscreen" :close-on-click-modal="false">

                <el-form ref="form" :model="form" :rules="rules" label-width="120px">


                    <el-form-item size="small" :label="$t('formValidation.samplingPlanName')" prop="planName">
                        <el-input v-model="form.planName" :placeholder="$t('tips.pleaseInput') + $t('formValidation.samplingPlanName')" />
                    </el-form-item>

                    <el-form-item size="small"  :label="$t('inspectionStandards.division')" prop="divisionName">
                        <division-dialog v-model="form.divisionName" :divisionId.sync="form.divisionId" @callback="cleardivisionName"/>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="cancel" size="small" icon="el-icon-close">{{$t('btn.cancel')}}</el-button>
                    <el-button :loading="loading" type="primary" @click="submitForm" size="small" icon="el-icon-check">{{ $t('btn.confirm') }}</el-button>
                </div>
            </el-dialog>
        </div>
    </list-layout>
</template>

<script>
import {samplingPlanaddAPI,samplingPlaneditAPI,samplingPlandelAPI, samplingPlanlistAPI} from '@/api/basic/transferRule'
import DivisionDialog from '@/views/components/DivisionDialog'

export default {
    name: 'Process',
    components: { DivisionDialog  },

    data() {
        return {
            fullscreen: false,
            // 组织
            organizesList: [],
            // 遮罩层
            loading: true,
            // 导出遮罩层
            exportLoading: false,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 产品线表格数据
            processList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,

                planName: null,
            },
            unitVisible: false,
            // 表单参数
            form: {
                planName:""
            },
            // 表单校验
            rules: {
                divisionName: [
                    {
                        required: true,
                        message: this.$t('formValidation.businessUnitCannotBeEmpty'),
                        trigger: 'blur',
                    },
                ],
                planName: [
                    {
                        required: true,
                        message:this.$t('formValidation.samplingPlanNameCannotBeEmpty'),

                    },
                ],
            },
        }
    },
    created() {
        this.getList()

    },
    methods: {

        cleardivisionName(){
            this.$refs.form.clearValidate('divisionName')
        },
        /** 查询产品线列表 */
        getList() {
            this.loading = true
            samplingPlanlistAPI(this.queryParams).then((response) => {
                this.processList = response.data
                this.total = response.total ? response.total :0
                this.loading = false
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                planName:'',
                divisionName:""
            }
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm')
            this.handleQuery()
        },

        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.open = true
            this.title = this.$t("public.add")
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.form = {
                planName:row.planName,
                divisionName:row.divisionName,
                divisionId:row.divisionId,
                id:row.id
            }
            this.open = true
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    this.loading = true;
                    if (this.form.id) {
                        samplingPlaneditAPI(this.form).then((response) => {
                            this.loading = false;
                            this.$modal.msgSuccess(
                                this.$t('tips.updateSuccess')
                            )
                            this.open = false
                            this.getList()
                        }).catch(() => {
                            this.loading = false;
                        })
                    } else {
                        samplingPlanaddAPI(this.form).then((response) => {
                            this.loading = false;
                            this.$modal.msgSuccess(this.$t('tips.addSuccess'))
                            this.open = false
                            this.getList()
                        }).catch(() => {
                            this.loading = false;
                        })
                    }
                }
            })
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id
            this.$modal
                .confirm(this.$t('formValidation.confirmToDelete'))
                .then(function () {
                    var data={
                        planName:row.planName,
                        divisionName:row.divisionName
                    }
                    return samplingPlandelAPI(data)
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgSuccess(this.$t('tips.deleteSuccess'))
                })
        },

    },
}
</script>
<style lang="scss" scoped>
::v-deep .el-card {
    padding-top: 20px;
    border: 0px;
    box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.08);
    border-radius: 4px;
    padding-bottom: 20px;
}
::v-deep .el-aside {
    padding: 0;
    margin-bottom: 0;
}
.content-style {
    border: 0px;
    box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.08);
    border-radius: 2px;
    background-color: white;
    margin-top: 10px;
}
::v-deep .el-button-group .selBtn {
    background: #fff;
    border: 1px solid #0b7be3;
    color: #0b7be3;
}
::v-deep .el-button-group .unselBtn {
    background: #fff;
    border: 1px solid #ededf2;
    color: #666666;
}
.header-left {
    font-size: 14px;
    font-family: PingFangSC-Semibold, PingFang SC;
    font-weight: 600;
    color: #333333;
    padding: 3px 0 10px;
    display: inline-flex;
    justify-content: center;
    border-bottom: 2px solid #0b7be3;
}
.header-right {
    float: right;
}
.container-header {
    border-bottom: 1px solid #ededf2;
}
.container-table {
    float: left;
    //margin-top:6px;
    width: 100%;
}
.mb8 {
    float: left;
    margin-top: 6px;
    width: 100%;
}
.drawer-footer {
    display: flex;
    position: relative;
    z-index: 9999;
    bottom: -40px;
    height: 40px;
    justify-content: center;
    width: 100%;
}
.drawer-close {
    background: #f7f8fa;
    border-color: #f7f8fa;
}

.right-btns ::v-deep .el-button {
    margin-top: 0;
}
.app-container {
    padding: 0;
}
.table-action-wrap {
    padding: 12px 0;
}
</style>
